package com.zh.note.huawei.onehundred.获得完美走位;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: zhanghuan
 * @date: 2023/8/7 21:08
 * @description: create it
 */
public class Main {
    public static void main(String[] args) {
        // Scanner sc = new Scanner(System.in);
        // String s = sc.nextLine();
        String s = "ASDWASDWASDWAAAA";
        if (s == null || s.length() < 1 || s.length() > 10 * 10 * 10 * 10) {
            System.out.println("0");
            return;
        }
        if (s.length() % 4 != 0) {
            System.out.println("0");
            return;
        }
        char[] chars = s.toCharArray();
        int length = chars.length;
        int number = length / 4;
        Map<Character, Integer> map = new HashMap<>();
        for (char ch : chars) {
            // s中只能含有ASDW
            if (ch != 'A' && ch != 'S' && ch != 'D' && ch != 'W') {
                System.out.println(0);
                return;
            }
            // 统计四种字符的次数
            map.put(ch, map.getOrDefault(ch, 0) + 1);
        }

        map.replaceAll((k, v) -> v - number);
        int resultValue = Integer.MAX_VALUE;

        for (int i = 0; i < length; i++) {
            char charOne = chars[i];
            int result = 0;
            Map<Character, Integer> hashMap = new HashMap<>(map);
            if (hashMap.get(charOne) > 0) {

                for (int j = i; j < length; j++) {
                    char c2 = s.charAt(j);
                    hashMap.put(c2, hashMap.get(c2) - 1);
                    result++;
                    boolean isFlag = true;
                    for (Integer val : hashMap.values()) {
                        if (val > 0) {
                            isFlag = false;
                        }
                    }
                    if (isFlag) {
                        break;
                    }
                }
            }
            boolean isFlag = true;
            for (Integer val : hashMap.values()) {
                if (val > 0) {
                    isFlag = false;
                }
            }
            if (isFlag) {
                resultValue = Math.min(result, resultValue);
            }
        }
        System.out.println(resultValue);
    }
}
